package org.spring.mybatis.mapper;

import org.apache.ibatis.annotations.*;
import org.spring.mybatis.model.UserInfo;

import java.util.List;

@Mapper
public interface UserInfoMapper {

    //查
    //写SQL语句
    @Select("SELECT * FROM `user_info`")
    List<UserInfo> selectList();


    @Results({
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime"),
    })
    @Select("SELECT * FROM `user_info`")
    List<UserInfo> selectList2();


    //参数传递
    @Select("select username,`password`,age,gender,phone from user_info where id=4")
    List<UserInfo> queryById();
    //id=4固定数值，查找数据不方便，改为动态数值 #{}
    @Select("select username,`password`,age,gender,phone from user_info where id=#{id}")
    UserInfo queryById2(Integer id);
    //@Param设置参数别名
    @Select("select username,`password`,age,gender,phone from user_info where id=#{userid}")
    UserInfo gueryById3(@Param("userid") Integer id);


    //增
    @Insert("insert into user_info (username,`password`,age,gender,phone)" +
            "values (#{username},#{password},#{age},#{gender},#{phone})")
    //返回受影响的行数
    Integer insert(UserInfo userInfo);
    //如果设置@Param属性，#{}需要使用参数.属性来获取
    @Insert("insert into user_info (username,`password`,age,gender,phone)" +
            "values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender},#{userInfo.phone})")
    //返回受影响的行数
    Integer insert2(@Param("userInfo") UserInfo userInfo);


    //自增id   添加Options注解
    @Insert("INSERT INTO user_info (password,username, age, gender, phone) VALUES (#{userInfo.password},#{userInfo.username}, #{userInfo.age}, #{userInfo.gender}, #{userInfo.phone})")
    @Options(useGeneratedKeys = true, keyProperty = "userInfo.id")
    Integer insert3(@Param("userInfo") UserInfo userInfo);



    //删
//    @Delete("delete from user_info where id = #{id}")
    @Delete("delete from user_info where id = ${id}")
    int delete(Integer id);



    //排序
    @Select("select id, username, age, gender, phone, delete_flag, create_time, update_time from user_info order by id ${sort}")
    List<UserInfo> queryAllUserBySort(String sort);




}
